<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者: 奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\user\controller;
class Bank extends Base
{
     public function initialize()
    {
       parent::initialize();
       $this->isLogin();
    }
    public function index()
    {
      //获得该用户已添加的所有银行卡
    $where['uid']  = session('uid');
    $list= model('userBank')->listData($where);

    $this->assign('list',$list);
      return $this->fetch();
    }
//添加银行卡
    public function addBank()
    {
    $param =$this->request->param();
   if($this->request->isAjax()){

//验证
        $rule =[
          'bank_type'   =>'require',
          'branch'     =>'require|chs',
          'card_number'=>'require|min:6',
          'realname'   =>'require|chsAlpha',
          'mobile'     =>'require|number',
        ];
        $msg= [
         'bank_type.require'   => '请选择银行卡',
         'branch.require'      => '请输入开户网点',
         'branch.chs'          => '请输入正确的开户网点',
         'card_number.require' => '请输入卡号',
         'card_number.min'     => '请输入至少6位数以上的卡号',
         'realname.require'    => '请输入真实姓名',
         'realname.chsAlpha'   => '请输入正确的姓名',
         'mobile.require'      => '请输入手机号',
         'mobile.number'       => '请输入正确的手机号',         
        ];

         $error = $this->checkSubmit($param,$rule,$msg);  
      
       if($error){
              return error_json($error);
         }
         //判断
         //
         $bank_num = model('userBank')->count(['uid'=>session('uid')]);
         if($bank_num >=9){
           $this->error('银行卡数量满额，请删除银行卡后再操作');
         }
         //判断当前银行卡是否存在
         $banklist = banklist();
         $data['uid']         = session('uid');
         $data['bankname']    = $banklist[$param['bank_type']]['name'];
         $data['bank_type']   = $param['bank_type'];
         $data['branch']      = $param['branch'];
         $data['lang']        = cookie('think_var'); //语言
         $data['card_number'] = $param['card_number'];
         $data['realname']    = $param['realname'];
         $data['bank_logo']    = $banklist[$param['bank_type']]['logo'];
         $data['mobile']      = $param['mobile'];
         $data['create_time']      = time();
         model('userBank')->addData($data);
         return  success_json('银行卡添加成功');
   }
     $getbanklist = banklist();
     $list = [];
    foreach($getbanklist as $k=>$v){

     if(!empty($this->settings['finance']['withdraw_bank'][$k])){
        $list[$k] = $v;
     }
    }
 
     $this->assign('banklist',$list);
     return $this->fetch();  
 }

		//银行卡取消绑定
		public function bankdel()
		{
		  $param  = $this->request->param();
		  
		  if($this->request->isAjax()){
		    $id = intval($param['id']);
		    if(!$id){
		         return error_json('请求参数错误');
		    }
		    $where =[];
		    $where['id']  = $id;
		    $where['uid'] = session('uid');   
		     $res= model('userBank')->delData($where);
		     if($res){
		       return success_json('银行卡取消绑定成功');
		     }
		  }

		}

}
